using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Library_management_system
{
    public partial class BookEditForm : Form
    {
        private int? bookId;
        public BookEditForm(int? id = null)
        {
            InitializeComponent();
            bookId = id;
            if (bookId.HasValue)
            {
                LoadBookInfo();
                this.Text = "修改图书信息";
            }
            else
            {
                this.Text = "添加图书信息";
            }
        }

        private void LoadBookInfo()
        {
            using (SqlConnection conn = DatabaseConfig.GetConnection())
            {
                conn.Open();
                string sql = "SELECT * FROM Books WHERE BookID=@id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", bookId);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    txtTitle.Text = reader["Title"].ToString();
                    txtAuthor.Text = reader["Author"].ToString();
                    txtPublisher.Text = reader["Publisher"].ToString();
                    txtISBN.Text = reader["ISBN"].ToString();
                    dtpPublishDate.Value = Convert.ToDateTime(reader["PublishDate"]);
                    txtCategory.Text = reader["Category"].ToString();
                    numTotalCount.Value = Convert.ToInt32(reader["TotalCount"]);
                    numAvailableCount.Value = Convert.ToInt32(reader["AvailableCount"]);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtTitle.Text))
            {
                MessageBox.Show("请输入书名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            using (SqlConnection conn = DatabaseConfig.GetConnection())
            {
                conn.Open();
                string sql;
                if (bookId.HasValue)
                {
                    sql = "UPDATE Books SET Title=@Title, Author=@Author, Publisher=@Publisher, ISBN=@ISBN, PublishDate=@PublishDate, Category=@Category, TotalCount=@TotalCount, AvailableCount=@AvailableCount WHERE BookID=@id";
                }
                else
                {
                    sql = "INSERT INTO Books (Title, Author, Publisher, ISBN, PublishDate, Category, TotalCount, AvailableCount) VALUES (@Title, @Author, @Publisher, @ISBN, @PublishDate, @Category, @TotalCount, @AvailableCount)";
                }
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
                cmd.Parameters.AddWithValue("@Author", txtAuthor.Text.Trim());
                cmd.Parameters.AddWithValue("@Publisher", txtPublisher.Text.Trim());
                cmd.Parameters.AddWithValue("@ISBN", txtISBN.Text.Trim());
                cmd.Parameters.AddWithValue("@PublishDate", dtpPublishDate.Value);
                cmd.Parameters.AddWithValue("@Category", txtCategory.Text.Trim());
                cmd.Parameters.AddWithValue("@TotalCount", numTotalCount.Value);
                cmd.Parameters.AddWithValue("@AvailableCount", numAvailableCount.Value);
                if (bookId.HasValue)
                {
                    cmd.Parameters.AddWithValue("@id", bookId);
                }
                cmd.ExecuteNonQuery();
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}